import { BookedService } from './../../services/booked.service';
import { OrtcService } from './../../services/ortc.service';
import { Booked, UserTypeX } from './../../entity/TT';
import { RequestReplyService } from 'src/app/services/request-reply.service';
import { Component, OnInit, Inject  } from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
import { NGXLogger } from 'ngx-logger';

export interface ConferenceListData {
  message: Booked[]
  userTypes: UserTypeX[];
}

@Component({
  selector: 'app-conference-list',
  templateUrl: './conference-list.component.html',
  styleUrls: ['./conference-list.component.scss']
})
export class ConferenceListComponent implements OnInit {

  setUserType : UserTypeX;
  setBooked  : Booked;
  bookedSn: string;

  constructor(
    private log: NGXLogger,
    public dialogRef: MatDialogRef<ConferenceListComponent>,
    @Inject(MAT_DIALOG_DATA) public data: ConferenceListData,
    private requestService: RequestReplyService,
    private rtcService: OrtcService,
    private bookedService: BookedService
  ) { }

  ngOnInit() {

  }

  onClose(): void {
    this.dialogRef.close();
  }

  doClickUserType(ut: UserTypeX): void{
    this.requestService.SetUserType(ut).subscribe(r => {
      if(r.success){
        this.setUserType = ut;
      }
    })
  }

  doClickBooked(book: Booked){
    this.requestService.SaveConference(book.sn).subscribe(r =>{
      // this.log.debug("=>", r);
      if (r.success) {
        this.setBooked = book;
        this.bookedSn = book.sn;
        }
    });
  }

  doYes(): void {
    this.log.debug('doYes');
    if (this.setBooked && this.setUserType) {
      this.rtcService.joinConference(this.bookedSn);
      this.bookedService.refresh();
      this.onClose();
    }

  }
}
